package com.zx.mes.hyl.auth.service.impl;

import com.zx.mes.hyl.auth.dao.ResourceRepository;
import com.zx.mes.hyl.auth.dao.ResourceTypeRepository;
import com.zx.mes.hyl.auth.dao.SysRepository;
import com.zx.mes.hyl.auth.model.Tresource;
import com.zx.mes.hyl.auth.pagemodel.Resource;
import com.zx.mes.hyl.auth.service.ResourceServiceI;
import com.zx.mes.hyl.common.pagemodel.LayPageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
/**
 * 资源逻辑服务类
 * @author hyl
 * @date 2017-12-20
 */
@Service
public class ResourceServiceImpl implements ResourceServiceI{

    @Autowired
    private ResourceRepository resourceRepository;

    @Autowired
    private SysRepository sysRepository;

    @Autowired
    private ResourceTypeRepository resourceTypeRepository;


    @Override
    public List<Resource> list(Resource resource, LayPageHelper ph) {
        List<Resource> list=new ArrayList<>();
        List resourceList=null;


        if(null==ph.getPage()){
            resourceList=resourceRepository.findAll();
        }else{
            Pageable pageable=new PageRequest(ph.getPage()-1,ph.getLimit());
            resourceList=resourceRepository.findAll(pageable).getContent();
        }

        listReturn(list, resourceList);
        return list;
    }

    private void listReturn(List list, List resourceList) {
        if (null!=resourceList && resourceList.size()>0){
            for (int i=0;i<resourceList.size();i++){
                Tresource t= (Tresource) resourceList.get(i);
                Resource r=new Resource();
                BeanUtils.copyProperties(t,r);
//                有父级
                if(t.getTresource()!=null){
                    r.setPid(t.getTresource().getId());
                    r.setPname(t.getTresource().getName());
                }
//                所属系统类型
                if (t.getTsys()!=null){
                    r.setSysId(t.getTsys().getId());
                    r.setSysName(t.getTsys().getName());
                }
//                资源类型
                if (t.getTresourcetype()!=null){
                    r.setTypeId(t.getTresourcetype().getId());
                    r.setTypeName(t.getTresourcetype().getName());
                }
                list.add(r);
            }
        }
    }

    @Override
    public Resource getById(String id) {
        Tresource tr=resourceRepository.getOne(id);
        Resource resource=new Resource();
        BeanUtils.copyProperties(tr,resource);
        //                有父级
        if(tr.getTresource()!=null){
            resource.setPid(tr.getTresource().getId());
            resource.setPname(tr.getTresource().getName());
        }
//                所属系统类型
        if (tr.getTsys()!=null){
            resource.setSysId(tr.getTsys().getId());
            resource.setSysName(tr.getTsys().getName());
        }
//                资源类型
        if (tr.getTresourcetype()!=null){
            resource.setTypeId(tr.getTresourcetype().getId());
            resource.setTypeName(tr.getTresourcetype().getName());
        }

        return resource;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(String id) {
        resourceRepository.delete(resourceRepository.getOne(id));
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(Resource resource) {
        Tresource tr=new Tresource();
        BeanUtils.copyProperties(resource,tr);
        resourceRepository.save(tr);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(Resource resource) {
        Tresource tr=new Tresource();
        BeanUtils.copyProperties(resource,tr);

        if (resource.getTypeId()!=null){
            tr.setTresourcetype(resourceTypeRepository.getOne(resource.getTypeId()));
        }

        if (resource.getSysId()!=null){
            tr.setTsys(sysRepository.getOne(resource.getSysId()));
        }
        if (resource.getPid()!=null && !"".equalsIgnoreCase(resource.getPid().trim())){
            tr.setTresource(resourceRepository.getOne(resource.getPid()));
        }

        resourceRepository.save(tr);
    }

    @Override
    public long count() {
        return resourceRepository.count();
    }
}
